package com.coocaa.ops.api.base.third.dmp.handler;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.coocaa.ops.api.base.cache.builder.CacheKeyClusterBuilder;
import com.coocaa.ops.api.base.cache.enums.InterfaceCacheMethodEnum;
import com.coocaa.ops.api.base.third.dmp.model.DmpCrowdModel;
import com.coocaa.ops.api.common.model.HeaderModel;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author tangminghong
 * @date 2020-09-04
 */
public class DmpHandler {

    public static String getDmpCrowdCacheKey(String mac, String did, String policyId) {
        return CacheKeyClusterBuilder.newBuilder(InterfaceCacheMethodEnum.getDmpCrowdCache)
                .withFilterKey("mac", mac)
                .withFilterKey("did", did)
                .withFilterKey("policy", policyId)
                .build();
    }


    public static Map<String, String> getDmpHeaderMap(HeaderModel header) {
        Map<String, String> dmpHeader = new HashMap<>();
        dmpHeader.put("cOpenId", header.getCOpenId());
        dmpHeader.put("vUserId", header.getVUserId());
        dmpHeader.put("thirdUserId", header.getThirdUserId());
        dmpHeader.put("MAC", header.getMac());
        dmpHeader.put("cAreaCode", header.getCAreaCode());
        dmpHeader.put("cBrand", header.getCBrand());
        dmpHeader.put("cChip", header.getCChip());
        return dmpHeader;
    }


    public static List<DmpCrowdModel> handleCrowdModels(String jsonStr, HeaderModel header) {
        if (StringUtils.isBlank(jsonStr) || null == header) {
            return null;
        }
        JSONObject jsonObject = JSON.parseObject(jsonStr);
        String rightCode = "1000";
        if (!rightCode.equals(jsonObject.getString("code"))) {
            return null;
        }
        JSONObject crowdData = jsonObject.getJSONObject("data");
        if (crowdData == null || CollectionUtils.isEmpty(crowdData.getJSONArray("result"))) {
            return null;
        }
        JSONArray resultList = crowdData.getJSONArray("result");
        List<DmpCrowdModel> crowds = new ArrayList<>();

        int index = 0;
        int size = resultList.size();
        for (; index < size; index++) {
            JSONObject result = resultList.getJSONObject(index);
            if (result != null) {
                Integer policyId = result.getInteger("policyId");
                Integer crowdId = result.getInteger("crowdId");
                if (null != policyId && null != crowdId) {
                    crowds.add(new DmpCrowdModel(policyId, crowdId));
                }
            }
        }
        return crowds;
    }

    public static List<Integer> parseCrowdIds(List<DmpCrowdModel> crowdModels) {
        List<Integer> crowdIds = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(crowdModels)) {
            for (DmpCrowdModel crowd : crowdModels) {
                crowdIds.add(crowd.getCrowdId());
            }
        }
        return crowdIds;
    }
}
